package com.szbmyoubao.app.view.dialog

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.szbmyoubao.app.bean.DateItem
import com.ellecity06.common.base.BaseBottomDialogFragment
import com.ellecity06.common.ktx.lastWeekStr
import com.ellecity06.common.ktx.lastYearStr
import com.ellecity06.common.ktx.toDateStr
import com.ellecity06.common.ktx.todayStr
import com.ellecity06.common.ktx.yesterdayStr
import com.ellecity06.common.widgets.dialog.showDatePicker
import com.ellecity06.cwhiskymall.databinding.DialogBottomSelectDateBinding

/**
 * @author zavier
 * @time 2025/3/6 20:14
 * @des 选择数字人
 */
class DateSelectDialog(
    val onSelectVoice: (DateItem) -> Unit
) : BaseBottomDialogFragment<DialogBottomSelectDateBinding>(),
    View.OnClickListener {
    private var type = 0
    override fun inflateBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ): DialogBottomSelectDateBinding {
        return DialogBottomSelectDateBinding.inflate(inflater, container, false)
    }


    private val dateList: List<DateItem> by lazy {
        mutableListOf(
            DateItem(startTime = lastYearStr(), endTime = todayStr(), "近30天"),
            DateItem(startTime = todayStr(), endTime = todayStr(), "今天"),
            DateItem(startTime = yesterdayStr(), endTime = todayStr(), "昨天"),
            DateItem(startTime = lastWeekStr(), endTime = todayStr(), "近7天")
        )
    }
    private var selectItem: DateItem = dateList[0]
    private var diyDate = DateItem(startTime = todayStr(), endTime = todayStr(), "自定义")
    override fun initialize() {
        setState()
        setDateState()
        initView()
        binding.tvPeople.setOnClickListener(this)
        binding.tvVideo.setOnClickListener(this)
        binding.ivClose.setOnClickListener(this)
        binding.tvConfirm.setOnClickListener(this)
        binding.tvDate1.setOnClickListener(this)
        binding.tvDate2.setOnClickListener(this)
        binding.tvDate3.setOnClickListener(this)
        binding.tvDate4.setOnClickListener(this)
        binding.tvStartTime.setOnClickListener(this)
        binding.tvEndTime.setOnClickListener(this)
    }

    private fun initView() {
        binding.tvDate1.text = dateList[0].title
        binding.tvDate2.text = dateList[1].title
        binding.tvDate3.text = dateList[2].title
        binding.tvDate4.text = dateList[3].title
        binding.tvStartTime.text = selectItem.startTime
        binding.tvEndTime.text = selectItem.endTime

    }

    private fun setDateState() {
        when (selectItem) {
            dateList[0] -> {
                binding.tvDate1.isSelected = true
                binding.tvDate2.isSelected = false
                binding.tvDate3.isSelected = false
                binding.tvDate4.isSelected = false
            }

            dateList[1] -> {
                binding.tvDate1.isSelected = false
                binding.tvDate2.isSelected = true
                binding.tvDate3.isSelected = false
                binding.tvDate4.isSelected = false
            }

            dateList[2] -> {
                binding.tvDate1.isSelected = false
                binding.tvDate2.isSelected = false
                binding.tvDate3.isSelected = true
                binding.tvDate4.isSelected = false
            }

            dateList[3] -> {
                binding.tvDate1.isSelected = false
                binding.tvDate2.isSelected = false
                binding.tvDate3.isSelected = false
                binding.tvDate4.isSelected = true
            }
        }
    }

    private fun setState() {
        if (type == 0) {
            // 选取视频
            binding.tvVideo.isChecked = true
            binding.tvPeople.isChecked = false
            binding.layout1.visibility = View.VISIBLE
            binding.layout2.visibility = View.GONE
        } else {
            // 选取数字人
            binding.tvVideo.isChecked = false
            binding.tvPeople.isChecked = true
            binding.layout1.visibility = View.GONE
            binding.layout2.visibility = View.VISIBLE
        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            binding.tvVideo.id -> {
                type = 0
                setState()
            }

            binding.tvPeople.id -> {
                type = 1
                setState()

            }

            binding.tvDate1.id -> {
                selectItem = dateList[0]
                setDateState()
            }

            binding.tvDate2.id -> {
                selectItem = dateList[1]
                setDateState()
            }

            binding.tvDate3.id -> {
                selectItem = dateList[2]
                setDateState()
            }

            binding.tvDate4.id -> {
                selectItem = dateList[3]
                setDateState()
            }

            binding.tvStartTime.id -> {
                showDatePicker {
                    diyDate.startTime = it.toDateStr()
                }
            }

            binding.tvEndTime.id -> {
                showDatePicker {
                    diyDate.endTime = it.toDateStr()
                }
            }

            binding.ivClose.id -> {
                dismiss()
            }

            binding.tvConfirm.id -> {
                if (type == 0) {
                    onSelectVoice.invoke(selectItem)
                } else {
                    onSelectVoice.invoke(diyDate)
                }
                dismiss()
            }
        }
    }
}